/* eslint-disable no-undef */
import requestParam from './params'
import { DES3 } from '@/assets/js/DES3.js'
export default {
  install: function (Vue) {
    Vue.prototype.ajaxRequest = function (url, params, method) {
      $.ajaxSetup({
        beforeSend: function (XMLHttpRequest) {
          var ifcToken = $.cookie('ifcToken')
          console.log(ifcToken)
          XMLHttpRequest.setRequestHeader('ifcToken', ifcToken)
        }
      })
      var PD = []
      var dtd = $.Deferred() // 新建一个Deferred对象
      for (var i = 0; i < PD.length; i++) {
        if (PD[i] === url) {
          //  console.log(params)
          // return dtd2.promise()
        }
      }
      console.log(params)
      PD.push(url)
      if (process.env.NODE_ENV === 'development') {
        url = '/iFengChao-App-Web/iFengChao-App-Web/' + url + requestParam
      } else {
        url = '/iFengChao-App-Web' + url + requestParam
      }
      if (method === undefined || method === '' || method === 'undefined') {
        method = 'POST'
      }
      const secret = '7$T5rcaOjA35bKoy&*dBbQS^ZPUsMDJj'
      params = JSON.stringify(params)
      var requestStr = DES3.encrypt(secret, params)
      let loading = this.$loading({text: '数据加载中...'})
      $.ajax({
        url: url,
        type: method,
        timeout: 10000,
        data: {'data': requestStr}
      }).then(function (result) {
        if (result) {
          loading.close()
          var decResult = DES3.decrypt(secret, result)
          var msg = JSON.parse(decResult)
          msg = JSON.parse(decResult)
          dtd.resolve(msg)
        }
        PD = []
      }, function (result) {
        loading.close()
        if (result) {
          var decResult = DES3.decrypt(secret, result)
          var msg = JSON.parse(decResult)
          dtd.reject(msg)
        }
        PD = []
      })
      return dtd.promise()
    }
  }
}
